import { Context } from 'koa';
import { Logger } from '../utils/logger';

const logger = Logger.getInstance();

export const requestLogger = async (ctx: Context, next: () => Promise<void>) => {
  const start = Date.now();

  await next();

  const duration = Date.now() - start;
  const { method, url, ip } = ctx;
  const { status } = ctx.response;

  logger.info(`${method} ${url}`, {
    statusCode: status,
    duration: `${duration}ms`,
    ip,
    userAgent: ctx.headers['user-agent'] || '',
  });
};